package com.cskaoyan.service;

import com.cskaoyan.bean.bo.AdminCommentDeleteBO;
import com.cskaoyan.bean.po.MarketComment;
import com.cskaoyan.bean.po.MarketCommentExample;
import com.cskaoyan.bean.vo.AdminCommentListVO;
import com.cskaoyan.mapper.MarketCommentMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author zhuqiushi
 * @since 2022/09/07 21:15
 */
@Service
public class AdminCommentServiceImpl implements AdminCommentService {
    @Autowired
    MarketCommentMapper marketCommentMapper;

    @Override
    public AdminCommentListVO list(Integer page, Integer limit, Integer valueId, Integer userId, String sort,
                                   String order) {
        PageHelper.startPage(page, limit);
        MarketCommentExample marketCommentExample = new MarketCommentExample();
        marketCommentExample.setOrderByClause(sort + " " + order);
        MarketCommentExample.Criteria criteria = marketCommentExample.createCriteria();
        criteria.andDeletedEqualTo(false);
        if (valueId != null) {
            criteria.andValueIdEqualTo(valueId);
        }
        if (userId != null) {
            criteria.andUserIdEqualTo(userId);
        }
        //调用commentMapper查询
        List<MarketComment> marketComments = marketCommentMapper.selectByExample(marketCommentExample);
        PageInfo<MarketComment> marketCommentPageInfo = new PageInfo<>(marketComments);
        AdminCommentListVO result = new AdminCommentListVO();
        result.setTotal((int) marketCommentPageInfo.getTotal());
        result.setPages(marketCommentPageInfo.getPages());
        result.setPage(page);
        result.setLimit(limit);
        result.setList(marketComments);
        return result;
    }

    @Override
    public int delete(AdminCommentDeleteBO adminCommentDeleteBO) {
        //直接将delete置为true，即为删除
        MarketComment marketComment = new MarketComment();
        marketComment.setId(adminCommentDeleteBO.getId());
        marketComment.setDeleted(true);
        marketCommentMapper.updateByPrimaryKeySelective(marketComment);
        return 0;
    }

}
